<?php

require_once('admincontroller.php');

class Candidats extends AdminController
{
	function Candidats()
	{
		parent::AdminController();
		$this->load->model('User');
		$this->load->model('UserStatus');
		$this->main_navigation = 'candidats';
		$this->data = array();
	}

	/**
	 * Index de gestion des candidats
	 */
	function index()
	{
		$this->sub_navigation =  'candidats';
		$data['users'] = $this->User->all('s.code != "admin" AND s.code != "former_student"');
		$this->display('user_list', $data);
	}

	function main_list()
	{
		$this->sub_navigation = 'main_list';
		$data['users'] = $this->User->all('s.code = "main_list"');
		$this->display('user_list', $data);
	}

	function user_edit($id)
	{
		/*$status_flow = array(
			'candidat_in_progress' => array('candidat_complete', 'rejected'),
			'candidat_complete' => array('forms_received', 'rejected'),
			'forms_received' => array('convoked', 'rejected'),
			'convoked' => array('main_list', 'additional_list', 'rejected'),
			'main_list' => array('rejected'),
			'additional_list' => array('rejected'),
			'rejected' => array(),
			'resignation' => array(),
			'former_student' => array()
		);*/
		$this->sub_navigation = 'candidats';
		$statuses = array();
		foreach ($this->UserStatus->all() as $status) {
			if ($status->code != 'admin') {
				$statuses[$status->id] = $status->label;
			}
		}
		$this->data['statuses'] = $statuses;
		$this->data['user'] = $this->User->get_by_id($id);
		$this->display('user_edit', $this->data);
	}

	function user_delete($id)
	{
		// don't delete yourself, idiot!
		if ($id != $this->session->userdata('user_id')) {
			if ($this->User->delete($id)) {
				return;
			}
		}
		$this->output->set_status_header(400);
	}

	function former_students($year = null)
	{
		$this->sub_navigation =  'former_students';
		$this->load->model('Promotion');
		$promotions = array();
		$promotions[''] = 'Tous';
		foreach ($this->Promotion->all() as $row) {
			$promotions[$row->year] = sprintf("Année scolaire %d/%d", $row->year - 1, $row->year);
		}
		$data['promotions'] = $promotions;
		$data['users'] = $this->User->former_students($year);
		$data['selected_promo'] = $year;
		$this->display('former_students', $data);
	}

	function get($id)
	{
		$data['status_history'] = $this->User->get_status_history($id);
		$user = $this->User->get_by_id($id);
		if ($user->status_code != 'admin') {
			$this->load->model('Insc');
			$data['insc_entries'] = $this->Insc->insc_entry_get_by_user_id($user->id);
		}
		$data['user'] = $user;
		$this->load->view('admin/candidats/user_details.php', $data);
	}

	function force_status($id)
	{
		$user = $this->User->plain_get($id);
		$old_status = $this->UserStatus->get_by_id($user->status_id);
		$old_status->label;


		// mise à jour du statut
		$this->User->set_status($user, $_POST['user_status'], $this->session->userdata('user_id'));

		if ($this->User->save($user)) {
			$this->data['success_notice'] = 'Statut mis à jour !';
			// envoi de l'email de notification
			if (isset($_POST['send_notification_email'])) {
				$this->load->library('email');
				$this->email->from(CONTACT_EMAIL);
				$this->email->to($user->email);
				$this->email->subject(EMAIL_SUBJECT_UPDATE_STATUS);
				$body = EMAIL_UPDATE_STATUS;
				parse_template($body, array(
					'user_firstname' => $user->firstname,
					'user_name' => $user->name,
					'old_status_name' => $old_status->label,
					'new_status_name' => $this->UserStatus->get_by_id($user->status_id)->label
				));
				$this->email->message($body);

				if ($this->email->send()) {
					$this->data['success_notice'] .= '<br />Un e-mail de notification a été envoyé au candidat';
				}
				else {
					$this->data['warning_notice'] = "L'envoi de l'e-mail de notification a échoué";
				}
			}
		}
		else {
			$this->data['error_notice'] = 'Erreur lors de la mise à jour du statut';
		}
		$this->user_edit($id);
	}

	function get_pdf($user_id)
	{
		$this->load->plugin('dompdf');

		$user = $this->User->get_by_id($user_id);
		$data['name'] = $user->name;
		$data['firstname'] = $user->firstname;
		$data['email'] = $user->email;

		$this->load->model('Insc');
		$user_fields = $this->Insc->insc_entry_get_by_user_id($user->id);
		foreach ($user_fields as $field) {
			$val_field = 'value_'.$field->field_type;
			$data[$field->field_id] = $field->$val_field;
		}

		// retrieving html from view template
		$html = $this->load->view('public/candidat/pdf_template', $data, true);

		// PDF stream (direct download)
		streampdf($html, strtolower($data['firstname'].'_'.$data['name']));
	}

	function promotions()
	{
		$this->load->model('Promotion');
		$this->data['current_promo'] = $this->Promotion->get_current();
		$this->data['promotions'] = $this->Promotion->all();
		$this->display('promotions', $this->data);
	}

	function active_next_promotion()
	{
		$this->load->model('Promotion');
		if ($this->Promotion->active_next()) {
			$this->data['success_notice'] = 'Nouvelle promotion ouverte';
		}
		else {
			$this->data['error_notice'] = 'Erreur lors de l\'ouverture de la nouvelle promotion';
		}
		$this->promotions();
	}
}
